// Created by liusc on 2020/7/28--11:17.
import pager from '@/assets/vue-mixin/pager'
import sortTree from '@/components/sort-tree'
import amendAdd from '../form/amend_add.vue'
import amendEdit from '../form/amend_edit.vue'
import {mapGetters} from "vuex"
import {fetchList, addObj, putObj, getObj, delObj} from "@/views/wy-budget/api/budgetamend.js";

export default {
    name: 'resource-manage',
    mixins: [pager],
    components: {sortTree, amendAdd, amendEdit},
    data() {
        return {
            tableData: [],
            searchForm: {},
            get_button:false,
            add_button: false,
            edit_button: false,
            del_button: false,
            placeholder: '请输入预算编号或者申请名称',
        }
    },

    computed: {
        ...mapGetters(['permissions']),
    },

    created() {
    },

    mounted(){
        this.get_button = this.permissions['amend_get']
        this.add_button = this.permissions['amend_add']
        this.edit_button = this.permissions['amend_edit']
        this.del_button = this.permissions['amend_del']
    },

    methods: {

        searchByKey(val){
            this.searchForm.budgetType = 11
            if (val) {
                this.searchForm.budgetCode = val
            } else {
                delete this.searchForm.budgetCode
            }
            this.resetList()
        },

        getListData() {
            this.searchForm.budgetType = 11
            fetchList(Object.assign({}, this.pageParams, this.searchForm)).then(resp => {
                this.setListData(resp.data)
            })
        },

        /**
         * 跳转到新增页面
         * @param scope
         */
        toAddData(scope) {
            getObj(scope.row.budgetId).then(respData => {
                this.$refs.amendAdd.show(respData.data.data);
            })
        },

        /**
         * 新增保存
         * @param data
         */
        addData(data) {
            addObj(data).then((data)=>{
                this.$alert('操作完成', '提示', {
                    confirmButtonText: '确定',
                });
                this.resetList();
            })
        },

        /**
         * 跳转到编辑页面
         * @param scope
         */
        toEditData(scope) {
            getObj(scope.row.budgetId).then(respData => {
                this.$refs.amendEdit.show(respData.data.data);
            })
        },

        /**
         * 修改保存
         * @param data
         */
        updateData(data) {
            putObj(data).then((data)=>{
                this.$alert('操作完成', '提示', {
                    confirmButtonText: '确定',
                });
                this.resetList();
            })
        },

        /**
         * 根据主键ID删除
         * @param scope
         */
        deleteRow(scope) {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                delObj(scope.row.budgetId).then(response => {
                    this.resetList()
                })

                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

    }
}
